#include"Animation.h"
#include"Defaul.h"
#include "Player.h"


Player::Player()
{
	img_shadow = new Animation(img_player_shadow, 45);
	anim_left_player = new Animation(atlas_player_left, 45);
	anim_right_player = new Animation(atlas_player_right, 45);
}

void Player::Move()
{
	int dir_x = is_move_right - is_move_left;
	int dir_y = is_move_down - is_move_up;
	float len_dir = sqrt(dir_x * dir_x + dir_y * dir_y);
	if (len_dir != 0)
	{
		float normalized_x = dir_x / len_dir;
		float normalized_y = dir_y / len_dir;
		player_pos_x += (int)(PLAYER_SPEED * normalized_x);
		player_pos_y += (int)(PLAYER_SPEED * normalized_y);
	}

	if (player_pos_x < 0) player_pos_x = 0;
	if (player_pos_y < 0) player_pos_y = 0;
	if (player_pos_x + PLAYER_WIDE > WINDOW_WIDE)player_pos_x = WINDOW_WIDE - PLAYER_WIDE;
	if (player_pos_y + PLAYER_HEIGHT > WINDOW_HEIGHT)player_pos_y = WINDOW_HEIGHT - PLAYER_HEIGHT;
}

void Player::Draw(int delta)
{
	int pos_shadow_x = player_pos_x + (PLAYER_WIDE / 2 - SHADOW_WIDTH / 2);
	int pos_shadow_y = player_pos_y + PLAYER_HEIGHT - 8;

	int dir_x = is_move_right - is_move_left;
	if (dir_x < 0)facing_left = true;
	else if (dir_x > 0)facing_left = false;

	if (facing_left) anim_left_player->Play(player_pos_x, player_pos_y, delta);
	else anim_right_player->Play(player_pos_x, player_pos_y, delta);
	img_shadow->Play(pos_shadow_x, pos_shadow_y,delta);

}

void Player::ProcessEvent(const ExMessage& msg)
{
	
		if (msg.message == WM_KEYDOWN)
		{
			switch (msg.vkcode)
			{
			case VK_LEFT:
				is_move_left = true;
				break;
			case VK_UP:
				is_move_up = true;
				break;
			case VK_RIGHT:
				is_move_right = true;
				break;
			case VK_DOWN:
				is_move_down = true;
				break;
			case 0x57:
				is_move_up = true;
				break;
			case 0x41:
				is_move_left = true;
				break;
			case 0x53:
				is_move_down = true;
				break;
			case 0x44:
				is_move_right = true;
				break;
			}
		}
		else if (msg.message == WM_KEYUP)
		{
			switch (msg.vkcode)
			{
			case VK_LEFT:
				is_move_left = false;
				break;
			case VK_UP:
				is_move_up = false;
				break;
			case VK_RIGHT:
				is_move_right = false;
				break;
			case VK_DOWN:
				is_move_down = false;
				break;
			case 0x57:
				is_move_up = false;
				break;
			case 0x41:
				is_move_left = false;
				break;
			case 0x53:
				is_move_down = false;
				break;
			case 0x44:
				is_move_right = false;
				break;
			}
		}
	
}
